const service = require('../../utils/service.js')
const api = require('../../utils/util.js')
Page({
  data: {
    shopData: {},
    order_id: '',
    orderInfo: {}
  },
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo');
    let shopData = wx.getStorageSync('shop_data_shop')
    this.setData({
      shopData: shopData,
      order_id: options.order_id,
      userInfo: userInfo
    })
    
    this.init()
  },

  init: function () {
    this.getOrderDetail()
  },

  // 获取订单详情
  getOrderDetail: function () {
    let that = this
    let order_id = this.data.order_id
    api.ajax(service.url('sellerOrderDetail'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      order_id: this.data.order_id
    }, function (res) {
      if (res.code == 0) {
        that.setData({
          orderInfo: res.data
        })
      } else {
        api.toast(res.message)
      }
    })
  },

  // 查看物流
  toExpress: function () {
    wx.navigateTo({
      url: '../shop_flow/flow?order_id=' + this.data.order_id,
    })
  },

  // 发货
  sendExpress: function () {
    wx.navigateTo({
      url: '../shop_send/send?order_id=' + this.data.order_id
    })
  },

  // 联系买家
  contact: function () {
    wx.showModal({
      title: '提示',
      content: '下载APP更方便联系买家，是否前往下载?',
      success: function (rel) {
        if (rel.confirm) {
          wx.navigateTo({
            url: '../download_app/download_app',
          })
        }
      }
    })
  },

  // 同意申请
  passRefun: function () {
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否同意退货退款申请?',
      success: function (rel) {
        if (rel.confirm) {
          api.ajax(service.url('agreeReturnOrder'), {
            unionid: that.data.shopData.shop_unionid,
            token: that.data.shopData.token,
            token_key: that.data.shopData.token_key,
            order_return_id: that.data.orderInfo.refund.order_return_id
          }, function (res) {
            if (res.code == 0) {
              api.toast('操作成功')
              getCurrentPages()[getCurrentPages().length - 2].onLoad()
              setTimeout(function () {
                wx.navigateBack({
                  delta: -1
                });
              }, 800)
            } else {
              api.toast(res.message)
            }
          })
        }
      }
    })
  },

  // 拒绝退款、退货
  noReturn: function () {
    let id = this.data.orderInfo.id
    wx.navigateTo({
      url: `../refuse_refun/refuse_refun?order_id=${id}`,
    })
  },

  // 删除订单
  delOrder: function () {
    let that = this
    wx.showModal({
      title: '提示',
      content: '确定删除该订单？',
      success: function (rel) {
        if (rel.confirm) {
          api.ajax(service.url('sellerorderDelOrder'), {
            unionid: that.data.shopData.shop_unionid,
            token: that.data.shopData.token,
            token_key: that.data.shopData.token_key,
            order_id: that.data.orderInfo.id
          }, function (res) {
            if (res.code == 0) {
              api.toast('删除成功')
              getCurrentPages()[getCurrentPages().length - 2].onLoad()
              setTimeout(function () {
                wx.navigateBack({
                  delta: -1
                });
              }, 800)
            } else {
              api.toast(res.message)
            }
          })
        }
      }
    })
  },

  // 前往商品详情
  toGoodsDetail: function (e) {
    let goods_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../goods_detail/goods_detail?goods_id=${goods_id}`,
    })
  }
})